package com.jintoufs.service.scene.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.scene.SceneMapper;
import com.jintoufs.dao.scene.SceneTicketMapper;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.scene.Scene;
import com.jintoufs.domain.scene.SceneTicket;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.domain.shop.ShopManager;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.scene.SceneService;
import com.jintoufs.service.shop.ShopManagerService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SceneServiceImpl implements SceneService {
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    private SceneMapper sceneMapper;

    @Resource
    private SceneTicketMapper sceneTicketMapper;
    @Resource
    private ShopService shopService;

    @Resource
    private ShopManagerService shopManagerService;

    @Override
    public Scene save(Scene scene) {
        sceneMapper.updateByPrimaryKeySelective(scene);
        return scene;
    }

    @Override
    public Map<String, Object> save(Scene scene, String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (ValidUtil.isEmpty(scene.getId())) {
                scene.setId(id);
                sceneMapper.insert(scene);
                result.put("msg", "新增成功");
            } else {
                sceneMapper.updateByPrimaryKeySelective(scene);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public Map<String, Object> save(Scene scene, String id, Customer customer) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (ValidUtil.isEmpty(scene.getId())) {
                scene.setId(id);
                scene.setStatus("0");
                scene.setCreateTime(new Date());
                scene.setModifyTime(new Date());
                sceneMapper.insert(scene);
                Shop shop = new Shop();
                shop.setName(scene.getSceneName());
                shop.setValueId(scene.getId());
                Shop now = shopService.save(shop,getUUID());
                ShopManager shopManager = new ShopManager();
                shopManager.setShopId(now.getId());
                shopManager.setCustomerId(customer.getId());
                shopManagerService.save(shopManager,getUUID());
                result.put("msg", "新增成功");
            } else {
                scene.setModifyTime(new Date());
                sceneMapper.updateByPrimaryKeySelective(scene);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public Map<String, Object> insert(Scene scene,String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (!ValidUtil.isEmpty(id)) {
                scene.setId(id);
                sceneMapper.insert(scene);
                result.put("msg", "新增成功");
            } else {
                sceneMapper.updateByPrimaryKeySelective(scene);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public List<Map<String, Object>> getScenesByCustomer(Customer customer) {
        return sceneMapper.selectByCustomer(customer.getId());
    }

    @Override
    @SystemServiceLog(description = "修改酒店信息")
    public void update(Scene scene) {
        sceneMapper.updateByPrimaryKey(scene);
    }

    @Override
    public List<Map<String, Object>> getRecommends(Map param) {
        return sceneMapper.getRecommends(param);
    }


    @Override
    @SystemServiceLog(description = "新增酒店")
    public Scene add(Scene scene) {
        Map<String, Object> params = new HashMap<>();
        params.put("sceneName", scene.getSceneName());
        List<Map<String,Object>> list = sceneMapper.selectByParams(params);
        if (list == null || list.size() == 0) {
            sceneMapper.insert(scene);
            return scene;
        } else {
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getScenesByParams(Map<String, Object> params) {
        return sceneMapper.selectByParams(params);
    }

    @Override
    public List<Map<String,Object>> selectByParams(Map<String, Object> params) {
        return sceneMapper.selectByParams(params);
    }


    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return sceneMapper.queryByParams(params);
    }


    @Override
    public List<Map<String, Object>> getTicketsByParams(Map<String, Object> params) {
        return sceneTicketMapper.selectByParams(params);
    }

    @Override
    public List<Map<String, Object>> queryPriceByParams(Map<String, Object> params) {
        return sceneTicketMapper.queryPriceByParams(params);
    }

    @Override
    @SystemServiceLog(description = "删除酒店")
    public void delete(String id) {
//        String[] ids = id.split(",");
//        for (String idm:ids){
        Map<String,Object> param = new HashMap<>();
        param.put("sceneId",id);
        List<Map<String,Object>> ticketList = sceneTicketMapper.selectByParams(param);
        for (Map<String,Object> ticket:ticketList){
//            sceneTicketMapper.deleteByPrimaryKey(ticket.get("id").toString());
            SceneTicket sceneTicket = sceneTicketMapper.selectByPrimaryKey(ticket.get("id").toString());
            sceneTicket.setStatus("0");
            sceneTicketMapper.updateByPrimaryKeySelective(sceneTicket);
        }
        Scene scene = sceneMapper.selectByPrimaryKey(id);
        scene.setStatus("0");
        sceneMapper.updateByPrimaryKeySelective(scene);
//        sceneMapper.deleteByPrimaryKey(id);
//        }
    }


    @Override
    public List<SceneTicket> getTicketList(String sceneId) {
        return sceneMapper.getTicketList(sceneId);
    }

    @Override
    public SceneTicket saveTicket(SceneTicket sceneTicket,String oper) {
        if ("insert".equals(oper)) {
            sceneTicketMapper.insertSelective(sceneTicket);
        } else {
            sceneTicketMapper.updateByPrimaryKeySelective(sceneTicket);
        }
        return sceneTicket;
    }


    @Override
    public List<Map<String, Object>> findByScene(Map<String, Object> params) {
        return sceneMapper.findByScene(params);
    }

    @Override
    public SceneTicket getTicketById(String ticketId) {
        return sceneTicketMapper.selectByPrimaryKey(ticketId);
    }

    @Override
    public int deleteTicketByid(String ticketId) {
        return sceneTicketMapper.deleteByPrimaryKey(ticketId);
    }

    @Override
    public List<Map<String,Object>> getByFilter(QueryFilters filters) {
        List<Map<String,Object>> result = sceneMapper.getByFilter(filters);
        return result;
    }

    @Override
    public Map<String,Object> getById(String id) {
        return sceneMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getBySearch(Map<String, Object> params) {
        return sceneMapper.getBySearch(params);
    }
    public String getUUID(){
        return String.valueOf(snowflakeIdWorker.nextId());
    }

    @Override
    public void unlock(String id) {
        Scene scene = sceneMapper.selectByPrimaryKey(id);
        switch (scene.getStatus()){
            case "0":
                scene.setStatus("1");
                break;
            case "1":
                scene.setStatus("0");
                break;
            default:
                break;
        }
        sceneMapper.updateByPrimaryKey(scene);
    }
    public Scene selectByPrimaryKey(String id){
        return sceneMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> getOnePriceOnScene(String sceneId) {
        return sceneMapper.getOnePriceOnScene(sceneId);
    }
}
